<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class LoginController extends Controller
{
    // 登录界面
    public function login()
    {
        return view('login');

    }

    /**
     * 登录处理 post
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request)
    {
        $data = $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|min:6'
        ]);
        if (\Auth::attempt($data)) {
            session()->flash('success', '登录成功');
            return redirect()->route('home');
        }
        session()->flash('danger', '邮箱或密码错误');
        return back();
    }

    // 退出
    public function logout()
    {
        \Auth::logout();
        session()->flash('success', '退出成功');
        return redirect()->route('home');
    }
}
